import Actor from "./Actor";
import ActorV from "./ActorV";
import BattleView from "./BattleView";

export default class BattleAniInterPreter{
    battleView:BattleView
    constructor(battleView:BattleView){
        this.battleView=battleView
    }
    aniList=null
    index=-1
    ani=null

    playAniList(aniList){
        this.aniList=aniList
        this.index=-1
        this.nextAni()
    }
    private nextAni(){
        this.index+=1
        if(this.aniList && this.aniList[this.index]){
            this.ani=this.aniList[this.index]
            this.playAni()
        }else{
            this.finishAniList()
        }
    }
    private playAni(){
        if(!this.ani) return
        console.log('开始播放ani',this.ani)
        if(this.ani.type==='arrangePos'){
            let arr=this.ani.changeArr
            arr.forEach(item=>{
               // const {teamId,gId,order}=item
                let teamId=item.teamId
                let gId=item.gId
                let order=item.order
                let actorV=this.battleView.actorVDic[gId] as ActorV
                if(actorV){
                    let pos=this.battleView.getPos(teamId,order)
                    Laya.Tween.to(actorV.spr,{x:pos.x,y:pos.y},300,null)
                }
            })
            Laya.timer.once(300,this,this.finishAni)
        }
        else if(this.ani.type==='attack'){
            let infos=this.ani.infos
            let actorV0=this.battleView.getActorV(infos[0].gId)
            let actorV1=this.battleView.getActorV(infos[1].gId)
            if(!actorV0 || !actorV1) {
                this.finishAni()
                return
            }
            let oPos0={
                x:actorV0.x,
                y:actorV0.y
            }
            let oPos1={
                x:actorV1.x,
                y:actorV1.y
            }
            let tPos0={
                x:(oPos0.x+oPos1.x)/2-10,
                y:oPos0.y-20
            }
            let tPos1={
                x:(oPos0.x+oPos1.x)/2+10,
                y:oPos1.y-20
            }
            Laya.Tween.to(actorV0,{x:tPos0.x,y:tPos0.y},200,null)
            Laya.Tween.to(actorV1,{x:tPos1.x,y:tPos1.y},200,null)
            Laya.timer.once(200,this,()=>{
                actorV0.setHp(infos[0].hp)
                actorV1.setHp(infos[1].hp)
                if(infos[0].kickOut){
                    actorV0.kickOut(-1)
                }else{
                    Laya.Tween.to(actorV0,{x:oPos0.x,y:oPos0.y},200,null)
                }
                if(infos[1].kickOut){
                    actorV1.kickOut(1)
                }else{
                    Laya.Tween.to(actorV1,{x:oPos1.x,y:oPos1.y},200,null)
                }

                Laya.timer.once(200,this,()=>{
                    this.finishAni()
                })
            })
        }else if(this.ani.type=='die'){
            this.ani.actors.forEach(gId=>{
                let actorV=this.battleView.getActorV(gId)
                if(actorV){
                    actorV.die()
                }
            })
            Laya.timer.once(300,this,()=>{
                this.ani.actors.forEach(gId=>{
                    delete this.battleView.actorVDic[gId]
                })
                this.finishAni()
            })
        }
    }
    private finishAni(){
        console.log("结束ani")
        this.ani=null
        this.nextAni()
    }

    private finishAniList(){
        this.aniList=null
        console.log("aniList播放完成")
    }

    





}